package com.example.zhujiemian.xuanke;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zhujiemian.R;

import java.util.List;

public class CourseAdapter extends RecyclerView.Adapter<CourseAdapter.CourseViewHolder> {
    private List<Course> courseList;

    public CourseAdapter(List<Course> courseList) {
        this.courseList = courseList;
    }

    @NonNull
    @Override
    public CourseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_course, parent, false);
        return new CourseViewHolder(itemView);
    }



    @Override
    public void onBindViewHolder(@NonNull CourseViewHolder holder, int position) {
        Course course = courseList.get(position);
        holder.courseNameTextView.setText(course.getCourseName());
        holder.isSelected = course.isSelected();

        // Update the checkbox state
        holder.courseCheckbox.setChecked(holder.isSelected);
    }

    @Override
    public int getItemCount() {
        return courseList.size();
    }

    public class CourseViewHolder extends RecyclerView.ViewHolder {
        public boolean isSelected;
        private TextView courseNameTextView;
        CheckBox courseCheckbox;

        public CourseViewHolder(@NonNull View itemView) {
            super(itemView);
            courseNameTextView = itemView.findViewById(R.id.tv_course_name);
            courseCheckbox = itemView.findViewById(R.id.cb_course_selected);

            // Set click listener for the checkbox
            courseCheckbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
                courseList.get(getAdapterPosition()).setSelected(isChecked);
            });
        }
    }
}